<template>
	<view class="">
		<u-navbar
			@rightClick="rightClick"
			:autoBack="true"
			rightText='搜索'
		>
			<view slot="center" class="search">
				<u-search 
				placeholder="请输入想要搜索的课程" 
				v-model="keyword"
				focus
				:showAction="false"
				:clearabled="false"
				height="0"
				@search="rightClick(keyword)"
				></u-search>
			</view>
		</u-navbar>
		<view class="line"></view>
		<!-- 历史搜索 -->
		<view class="search-history" v-if="historyList.length">
			<view class="search-title">
				<view class="search-text">
					历史搜索
				</view>
				<view @click="removeHistory">
					<u-icon name="trash" size="22"></u-icon>
				</view>
			</view>
			<view class="history-list">
				<view
				  v-for="(item,index) in historyList"
				  @click="rightClick(item)"
				>{{ item }}</view>
			</view>
		</view>
		<!-- 热门搜索 -->
		<view class="search-hot">
			<view class="search-hot-title">
				<view>
					<u-icon name="search" size="24"></u-icon>
				</view>
				<view class="search-text">
					热门搜索
				</view>
			</view>
			<view class="search-hot-list">
				<view 
					v-for="item,idx in hotKeywords" 
					:key="item.id" 
					style="display: flex;"
					@click="rightClick(item.keywords)"
					>
					{{ item.keywords }}
					<u-icon name="photo" v-if="idx < 2"></u-icon>
				</view>
				
			</view>
		</view>
	</view>
</template>

<script>
	// 引入请求热搜词接口函数
	import { getHotKeywords } from '../../api/search'
	
	export default {
		data() {
			return {
				keyword:'',
				historyList:JSON.parse(uni.getStorageSync('historySearch')),
				hotKeywords:[]
			}
		},
		methods:{
			rightClick(key) {
				let k = this.keyword || key
				
				if (!(k.trim())) return
				// 判断keyword值是否在这个数组中存在
				let idx = this.historyList.indexOf(k)
				if (idx < 0) {
					// 如果不存在那么添加
					this.historyList.unshift(k)
				}else {
					// 如果存在那么让该数据在第一位
					// this.historyList.splice(idx,1) 值为删除的元素单独组成一个数组，如重复的为a，那么就是["a"]
					this.historyList.unshift(this.historyList.splice(idx,1)[0])
				}
				// 数据持久化
				uni.setStorageSync('historySearch',JSON.stringify(this.historyList))
				// 跳转到搜索列表组件
				uni.navigateTo({
					url:`/pages/search/search-list?key=${k}`
				})
			},
			// 清空历史记录
			removeHistory() {
				let that = this
				uni.showModal({
					title:'您确认删除所有的历史记录吗?',
					success(e) {
						if (e.confirm) {
							that.historyList = []
							uni.setStorageSync('historySearch',JSON.stringify([]))
						}
					}
				})
			}
		},
		onLoad() {
			getHotKeywords().then(res => {
				this.hotKeywords = res.list
			})
		}
	}
</script>

<style>
	.search {
		width: 527rpx;
	}
	/deep/.u-search__content {
		padding: 6rpx 30rpx;
	}
	.line {
		width: 100%;
		height: 4rpx;
		background: rgba(241,241,241,0.39);
		margin-top: 120rpx;
	}
	.search-history,.search-hot {
		padding: 33rpx;
	}
	.search-text {
		font-size: 28rpx;
		font-weight: bold;
		color: #636363;
	}
	.search-title {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	/* 搜索记录的历史记录 */
	.history-list {
		display: flex;
		flex-wrap: wrap;
	}
	.history-list > view {
		padding: 5px 10px;
		background-color: #e6e6e6;
		border-radius: 5px;
		margin: 10rpx 0px;
	}
	.history-list > view:nth-child(2n) {
		margin-left: 30rpx;
		margin-right: 30rpx;
	}
	/* 热门搜索 */
	.search-hot-title {
		display: flex;
		align-items: center;
		padding-bottom: 33rpx;
		border-bottom: 1px solid #ccc;
	}
	.search-hot-title > view {
		margin-right: 10rpx;
	}
	.search-hot-list {
		display: flex;
		flex-wrap: wrap;
	}
	.search-hot-list > view {
		width: 50%;
		font-size: 29rpx;
		color: #333;
		font-weight: 400;
		padding-top: 33rpx;
	}
</style>